
function dangnhap() {
    var email = document.getElementById("email").value;
    var pass = document.getElementById("password").value;
    if (email == '') {
        alert("Chưa nhập Email");
    }
    else if (pass == '') {
        alert("Chưa nhập mật khẩu");

    }
    else {
        Login(email, pass);
    }
}

function Login(Email, Password) {
    var webMethod = '/WebService.asmx/Login'
    var parameters = "{'Email':'" + Email + "','Password':'" + Password + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == true) {
                window.location.reload();
                document.getElementById("email").value = "";
            }
            else {

                alert("Đăng nhập không thành công");

            }
        }

    });
};

function logout() {
    var webMethod = '/WebService.asmx/Logout'
    var parameters = "";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == true) {
                $("#msgdangnhap").empty().html(msg.d);
                window.location.reload();
            }
        }
    });
};
function register_reset() {
    document.getElementById("Hoten").value = "";
    document.getElementById("Email").value = "";
    document.getElementById("Matkhau").value = "";
    document.getElementById("Confim").value = "";
    document.getElementById("Dienthoai").value = "";
    document.getElementById("Diachi").value = "";
}

function register_submit() {

    var hoten = document.getElementById("Hoten").value;
    var email = document.getElementById("Email").value;
    var matkhau = document.getElementById("Matkhau").value;
    var confim = document.getElementById("Confim").value;
    var dienthoai = document.getElementById("Dienthoai").value;
    var diachi = document.getElementById("Diachi").value;

    if (hoten == "" || email == "" || matkhau == "" || dienthoai == "") {
        alert("Bạn phải nhập đầy đủ thông tin");

    }
    else {
        if (matkhau == confim) {
            register(hoten, email, matkhau, dienthoai, diachi);
        }
        else {
            alert("Mật khẩu không trùng");
        }
    }
}

function register(Hoten, Email, Matkhau, Dienthoai, Diachi) {
    var webMethod = '/WebService.asmx/Register'
    var parameters = "{'Hoten':'" + Hoten + "','Email':'" + Email + "','Matkhau':'" + Matkhau + "','Dienthoai':'" + Dienthoai + "','Diachi':'" + Diachi + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            $("#msg").empty().html(msg.d);

            if (msg.d == "Đăng ký thành công") {
                document.getElementById("Hoten").value = "";
                document.getElementById("Email").value = "";
                document.getElementById("Matkhau").value = "";
                document.getElementById("Confim").value = "";
                document.getElementById("Dienthoai").value = "";
                document.getElementById("Diachi").value = "";
                window.location.reload();
            }
        },
        error: function (e) {
            $("#msg").html("Unavailable");
        }
    });
};

/*=================Gui Don Hang===================*/

function guidonhang() {

    var hoten = document.getElementById("NguoiGui").value;
    var email = document.getElementById("Email").value;
    var dienthoai = document.getElementById("DienThoai").value;
    var diachi = document.getElementById("DiaChi").value;
    var chitiet = document.getElementById("ChiTiet").value;
    if (hoten == "" || email == "" || dienthoai == "" || diachi == "") {
        alert("Bạn phải nhập đầy đủ thông tin");

    }
    else {
        guidonhang_server(hoten, email, dienthoai, diachi, chitiet);
    }

}




function guidonhang_server(NguoiGui, Email, Dienthoai, Diachi, ChiTiet) {
    var webMethod = '/WebService.asmx/guidonhang'
    var parameters = "{'NguoiGui':'" + NguoiGui + "','Email':'" + Email + "','Dienthoai':'" + Dienthoai + "','Diachi':'" + Diachi + "','ChiTiet':'" + ChiTiet + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("#msgDathang").empty().html(msg.d);
            if (msg.d == "Mua hàng thành công") {
                document.getElementById("NguoiGui").value = "";
                document.getElementById("Email").value = "";
                document.getElementById("DienThoai").value = "";
                document.getElementById("DiaChi").value = "";

                document.getElementById("ChiTiet").value = "";
                window.location.reload();
            }
        },
        error: function (e) {
            $("#msgDathang").html("Unavailable");
        }
    });
};

function guithongtin() {

    var hoten = document.getElementById("TTNguoiGui").value;
    var email = document.getElementById("TTEmail").value;
    var dienthoai = document.getElementById("TTDienThoai").value;
    var diachi = document.getElementById("TTDiaChi").value;
    var chitiet = document.getElementById("TTChiTiet").value;
    if (hoten == "" || email == "" || dienthoai == "" || diachi == "") {
        alert("Bạn phải nhập đầy đủ thông tin");

    }
    else {
        guithongtin_server(hoten, email, dienthoai, diachi, chitiet);
    }

}
function guithongtin_server(NguoiGui, Email, Dienthoai, Diachi, ChiTiet) {
    var webMethod = '/WebService.asmx/guithongtin'
    var parameters = "{'NguoiGui':'" + NguoiGui + "','Email':'" + Email + "','Dienthoai':'" + Dienthoai + "','Diachi':'" + Diachi + "','ChiTiet':'" + ChiTiet + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("#msgGuiThongTin").empty().html(msg.d);
            if (msg.d == "Gửi thông tin thành công") {
                document.getElementById("TTNguoiGui").value = "";
                document.getElementById("TTEmail").value = "";
                document.getElementById("TTDienThoai").value = "";
                document.getElementById("TTDiaChi").value = "";

                document.getElementById("TTChiTiet").value = "";
                window.location.reload();
            }
        },
        error: function (e) {
            $("#msgGuiThongTin").html("Unavailable");
        }
    });
};
function guithongtin_reset() {
    document.getElementById("TTNguoiGui").value = "";
    document.getElementById("TTEmail").value = "";
    document.getElementById("TTDienThoai").value = "";
    document.getElementById("TTDienThoai").value = "";
    document.getElementById("TTDiaChi").value = "";
    document.getElementById("TTChiTiet").value = "";
}

/*================Thong tin dat hang online==========*/
function guithongtindathang() {

    var hoten = document.getElementById("TTDHNguoiGui").value;
    var email = document.getElementById("TTDHEmail").value;
    var dienthoai = document.getElementById("TTDHDienThoai").value;
    var diachi = document.getElementById("TTDHDiaChi").value;
    var chitiet = document.getElementById("TTDHChiTiet").value;
    if (hoten == "" || email == "" || dienthoai == "" || diachi == "") {
        alert("Bạn phải nhập đầy đủ thông tin");

    }
    else {
        guithongtindathang_server(hoten, email, dienthoai, diachi, chitiet);
    }

}
function guithongtindathang_server(NguoiGui, Email, Dienthoai, Diachi, ChiTiet) {
    var webMethod = '/WebService.asmx/guithongtin'
    var parameters = "{'NguoiGui':'" + NguoiGui + "','Email':'" + Email + "','Dienthoai':'" + Dienthoai + "','Diachi':'" + Diachi + "','ChiTiet':'" + ChiTiet + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $("#msgGuiThongTinDatHang").empty().html(msg.d);
            if (msg.d == "Gửi thông tin thành công") {
                document.getElementById("TTDHNguoiGui").value = "";
                document.getElementById("TTDHEmail").value = "";
                document.getElementById("TTDHDienThoai").value = "";
                document.getElementById("TTDHDiaChi").value = "";

                document.getElementById("TTDHChiTiet").value = "";
                window.location.reload();
            }
        },
        error: function (e) {
            $("#msgGuiThongTinDatHang").html("Unavailable");
        }
    });
};
function guithongtindathang_reset() {
    document.getElementById("TTDHNguoiGui").value = "";
    document.getElementById("TTDHEmail").value = "";
    document.getElementById("TTDHDienThoai").value = "";
    document.getElementById("TTDHDienThoai").value = "";
    document.getElementById("TTDHDiaChi").value = "";
    document.getElementById("TTDHChiTiet").value = "";
}

/*===================QUOC GIA - TINH?TP=======*/

function DanhMucChange() {
    var id = document.getElementById('ddlDM').value;

    if (id == null || id == "") {

    }
    else {
        GetDanhMucCon(id);
    }



};
function GetDanhMucCon(MaLoaiDM) {
    //    alert(MaQuocGia);
    var webMethod = '/WebService.asmx/GetDanhMucCon'
    var parameters = "{'MaLoaiDM':'" + MaLoaiDM + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            var i = 0;
            var result = "";
            $.each(msg, function (key, val) {
                var data = jQuery.parseJSON(val);
                for (i = 0; i < data.length; i++) {
                    result += '<option value="' + data[i].Id + '">' + data[i].Name + '</option>';
                }
            })
            $("#ddlDMCon").empty().html(result);
        },
        error: function (e) {
            $("#ddlDMCon").html("Unavailable");
        }
    });
};

//====================FORM LIEN HE================
function removeSpaces(string) {
    return string.split(' ').join('');
}
function lienhe_submit() {
    var flag = true;
    var F = document.forms[0];
    var hoten = document.getElementById("HoTen_lh").value;
    var email = document.getElementById("Email_lh").value;
    var dienthoai = document.getElementById("SoDienThoai_lh").value;
    var diachi = document.getElementById("DiaChi_lh").value;
    var chitiet = document.getElementById("ChiTiet_lh").value;

    var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
    var str2 = removeSpaces(document.getElementById('inputCaptcha').value);

    if (hoten == "") {
        alert("Chưa nhập họ tên");
        F.HoTen_lh.focus();
        flag = false;
    }
    else if (email == "") {
        alert("Chưa nhập email");
        F.Email_lh.focus();
        flag = false;
    }
    else if (dienthoai == "") {
        alert("Chưa nhập điện thoại");
        F.SoDienThoai_lh.focus();
        flag = false;
    }
    else if (diachi == "") {
        alert("Chưa nhập địa chỉ");
        F.DiaChi_lh.focus();
        flag = false;
    }
    else {
        if (str1 == str2) {
            formlienhe(hoten, email, dienthoai, diachi, chitiet);
        } else {
            alert("Mã xác nhận không trùng");
            F.inputCaptcha.focus();
            flag = false;

        }
    }

}

function formlienhe(HoTen_lh, Email_lh, SoDienThoai_lh, DiaChi_lh, ChiTiet_lh) {


    var webMethod = '/WebService.asmx/FormLienHe'
    var parameters = "{'HoTen_lh':'" + HoTen_lh + "','Email_lh':'" + Email_lh + "','SoDienThoai_lh':'" + SoDienThoai_lh + "','DiaChi_lh':'" + DiaChi_lh + "','ChiTiet_lh':'" + ChiTiet_lh + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            $("#msglienhe").empty().html(msg.d);

            if (msg.d == "Gửi thông tin liên hệ thành công") {
                document.getElementById("HoTen_lh").value = "";
                document.getElementById("Email_lh").value = "";
                document.getElementById("SoDienThoai_lh").value = "";
                document.getElementById("DiaChi_lh").value = "";
                document.getElementById("ChiTiet_lh").value = "";
                document.getElementById("inputCaptcha").value = "";
                window.location.reload();
            }
        },
        error: function (e) {
            $("#msglienhe").html("Unavailable");
        }
    });

};


/*===================goi lai cho toi===================*/
function goilaichotoi_submit() {
    var flag = true;
    var F = document.forms[0];
    var hoten = document.getElementById("HoTen_box").value;
    var email = document.getElementById("Email_box").value;
    var chitiet = document.getElementById("NoiDung_box").value;

    if (hoten == "") {
        alert("Chưa nhập họ tên");
        F.HoTen_box.focus();
        flag = false;
    }
    else if (email == "") {
        alert("Chưa nhập số phone");
        F.Email_box.focus();
        flag = false;
    }
    else if (chitiet == "") {
        alert("Chưa nhập nội dung");
        F.NoiDung_box.focus();
        flag = false;
    }

    else {

        box_GoiLaiChoToi(hoten, email, chitiet);
    }

}

function box_GoiLaiChoToi(HoTen_box, Email_box, NoiDung_box) {


    var webMethod = '/WebService.asmx/FormGoiLaiChoToi'
    var parameters = "{'HoTen_box':'" + HoTen_box + "','Email_box':'" + Email_box + "','NoiDung_box':'" + NoiDung_box + "'}";
    $.ajax({
        type: "POST",
        url: webMethod,
        data: parameters,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {

            $("#msg_goilaichotoi").empty().html(msg.d);

            if (msg.d == "Gửi thông tin thành công") {
                document.getElementById("HoTen_box").value = "";
                document.getElementById("Email_box").value = "";              
                document.getElementById("NoiDung_box").value = "";              
               
            }
        },
        error: function (e) {
            $("#msg_goilaichotoi").html("Unavailable");
        }
    });

};